package com.huaxin.hxmoduleworkorder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaxin.feignclient.websocket.models.SendMessageRequest;
import com.huaxin.hxmoduleworkorder.common.enums.InspectionReadStatus;
import com.huaxin.hxmoduleworkorder.common.enums.InspectionSendStatus;
import com.huaxin.hxmoduleworkorder.dto.ResponseCode;
import com.huaxin.hxmoduleworkorder.dto.ResponseResult;
import com.huaxin.hxmoduleworkorder.dto.request.GetOrderNotificationInSystemByUserIdReqDTO;
import com.huaxin.hxmoduleworkorder.dto.request.OrderNotificationCreateReqDTO;
import com.huaxin.hxmoduleworkorder.mapper.OrderNotificationMapper;
import com.huaxin.hxmoduleworkorder.mapping.OrderNotificationMapping;
import com.huaxin.hxmoduleworkorder.models.OrderNotification;
import com.huaxin.hxmoduleworkorder.service.OrderNotificationService;
import com.huaxin.hxrabbitmq.service.MessageSender;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class OrderNotificationServiceImpl extends ServiceImpl<OrderNotificationMapper, OrderNotification> implements OrderNotificationService {
    private final ApplicationContext applicationContext;
    private final OrderNotificationMapping mapping;
    private final MessageSender messageSender;

    @Override
    public boolean create(OrderNotificationCreateReqDTO in) {
        if (in == null) {
            return false;
        }
        if (StringUtils.isBlank(in.getOrderId())) {
            return false;
        }
        if (StringUtils.isBlank(in.getRecipientId())) {
            return false;
        }
        if (StringUtils.isBlank(in.getRecipientRole())) {
            return false;
        }
        if (StringUtils.isBlank(in.getNotificationType())) {
            return false;
        }
        if (StringUtils.isBlank(in.getContent())) {
            return false;
        }
        OrderNotification entity = mapping.toEntity(in);
        entity.setSendStatus(InspectionSendStatus.SENT);
        entity.setReadStatus(InspectionReadStatus.UNREAD);
        entity.setSendTime(LocalDateTime.now());
        boolean result = this.save(entity);
        if (result) {
            //调用websocket向指定电工推送消息
            SendMessageRequest request = new SendMessageRequest();
            request.setTo(entity.getRecipientId());
            request.setContent(entity.getContent());
            messageSender.send(request);
        }
        return result;
    }

    /**
     * 查询工单通知列表
     *
     * @param in
     * @return
     */
    @Override
    public ResponseResult<Page<OrderNotification>> getOrderNotificationInSystemByUserId(GetOrderNotificationInSystemByUserIdReqDTO in) {
        if (in == null) {
            return ResponseResult.failure(ResponseCode.BAD_REQUEST, "参数错误", null);
        }
        Page<OrderNotification> page = new Page<>(in.getPageNum(), in.getPageSize());
        LambdaQueryWrapper<OrderNotification> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(in.getOrderId())) {
            queryWrapper.eq(OrderNotification::getOrderId, in.getOrderId());
        }
        if (StringUtils.isNotBlank(in.getOrderElectricianTaskId())) {
            queryWrapper.eq(OrderNotification::getOrderElectricianTaskId, in.getOrderElectricianTaskId());
        }
        if (StringUtils.isNotBlank(in.getUserId())) {
            queryWrapper.eq(OrderNotification::getRecipientId, in.getUserId());
        }
        if (StringUtils.isNotBlank(in.getOrderType())) {
            queryWrapper.eq(OrderNotification::getOrderType, in.getOrderType());
        }
        if (StringUtils.isNotBlank(in.getReadStatus())) {
            queryWrapper.eq(OrderNotification::getReadStatus, in.getReadStatus());
        }
        if (StringUtils.isNotBlank(in.getSendStatus())) {
            queryWrapper.eq(OrderNotification::getSendStatus, in.getSendStatus());
        }
        return ResponseResult.ok(this.page(page, queryWrapper));
    }

    /**
     * 获取工单通知详情
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult<OrderNotification> getOrderNotificationById(String id) {
        if (StringUtils.isBlank(id)) {
            return ResponseResult.parameterError(null, "ID不能为空");
        }
        return ResponseResult.ok(this.getById(id));
    }

    /**
     * 更新工单通知发送状态
     *
     * @param id
     * @param sendStatus
     * @return
     */
    @Override
    public boolean updateOrderNotificationSendStatus(String id, InspectionSendStatus sendStatus) {
        if (StringUtils.isBlank(id)) {
            return false;
        }
        if (sendStatus == null) {
            return false;
        }
        OrderNotification entity = this.getById(id);
        if (entity == null) {
            return false;
        }
        entity.setSendStatus(sendStatus);
        return this.updateById(entity);
    }

    /**
     * 获取工单通知详情
     *
     * @param id
     * @return
     */
    @Override
    public boolean updateOrderNotificationReadStatus(String id, InspectionReadStatus readStatus) {
        if (StringUtils.isBlank(id)) {
            return false;
        }
        if (readStatus == null) {
            return false;
        }
        OrderNotification entity = this.getById(id);
        if (entity == null) {
            return false;
        }
        entity.setReadStatus(readStatus);
        return this.updateById(entity);
    }
}